from wormbane.geometry import Box, Vector
from wormbane.Tile import Tile
from heapq import heapify, heappop, heappush


class Level(object):

    def __init__(self):
        self.size = Vector(79, 22)
        self.bounds = Box(Vector(0, 0), self.size)
        self.tiles = [[Tile(Vector(x, y), self) for y in xrange(self.size.y)]
                      for x in xrange(self.size.x)]
        self.rooms = []
        self.monsters = []
        self.time = 0.0
        self.stair_up_tile = None
        self.stair_down_tile = None
        self.num = 1

    def push_monster(self, monster):
        heappush(self.monsters, _MonsterWrapper(monster))
        self.time = self.monsters[0].monster.time

    def pop_monster(self):
        return heappop(self.monsters).monster

    def remove_monster(self, monster):
        for i, wrapper in enumerate(self.monsters):
            if wrapper.monster is monster:
                del self.monsters[i]
                heapify(self.monsters)
                return


class _MonsterWrapper(object):
    def __init__(self, monster):
        self.monster = monster

    def __cmp__(self, other):
        return cmp(self.monster.time, other.monster.time)
